<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>常用的Lambda表达式 | 牵风散步の雲</title><meta name="author" content="牵风散步の雲"><meta name="copyright" content="牵风散步の雲"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="1. 使用Lambda表达式进行集合遍历 未使用Lambda表达式：  1234List&lt;String&gt; list &#x3D; Arrays.asList(&quot;apple&quot;, &quot;banana&quot;, &quot;orange&quot;);for (String fruit : list) &amp;#123;    System.out.println(fruit)">
<meta property="og:type" content="article">
<meta property="og:title" content="常用的Lambda表达式">
<meta property="og:url" content="https://kirito2493381254.gitee.io/kirito/2023/05/05/%E5%B8%B8%E7%94%A8%E7%9A%84Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F/index.html">
<meta property="og:site_name" content="牵风散步の雲">
<meta property="og:description" content="1. 使用Lambda表达式进行集合遍历 未使用Lambda表达式：  1234List&lt;String&gt; list &#x3D; Arrays.asList(&quot;apple&quot;, &quot;banana&quot;, &quot;orange&quot;);for (String fruit : list) &amp;#123;    System.out.println(fruit)">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://kirito2493381254.gitee.io/kirito/img/artical.jpg">
<meta property="article:published_time" content="2023-05-05T06:57:31.895Z">
<meta property="article:modified_time" content="2023-08-02T04:59:57.848Z">
<meta property="article:author" content="牵风散步の雲">
<meta property="article:tag" content="Java基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://kirito2493381254.gitee.io/kirito/img/artical.jpg"><link rel="shortcut icon" href="/kirito/img/favicon.png"><link rel="canonical" href="https://kirito2493381254.gitee.io/kirito/2023/05/05/%E5%B8%B8%E7%94%A8%E7%9A%84Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/kirito/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/kirito/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  dateSuffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '常用的Lambda表达式',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-08-02 12:59:57'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/kirito/atom.xml" title="牵风散步の雲" type="application/atom+xml">
</head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/kirito/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/kirito/archives/"><div class="headline">Articles</div><div class="length-num">21</div></a><a href="/kirito/tags/"><div class="headline">Tags</div><div class="length-num">12</div></a><a href="/kirito/categories/"><div class="headline">Categories</div><div class="length-num">8</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/kirito/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/archives/"><i class="fa-fw fas fa-archive"></i><span> 時間軸</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/tags/"><i class="fa-fw fas fa-tags"></i><span> 標籤</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分類</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清單</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/kirito/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/kirito/link/"><i class="fa-fw fas fa-link"></i><span> 友鏈</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/kirito/img/artical.jpg')"><nav id="nav"><span id="blog-info"><a href="/kirito/" title="牵风散步の雲"><span class="site-name">牵风散步の雲</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/kirito/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/archives/"><i class="fa-fw fas fa-archive"></i><span> 時間軸</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/tags/"><i class="fa-fw fas fa-tags"></i><span> 標籤</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分類</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清單</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/kirito/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/kirito/link/"><i class="fa-fw fas fa-link"></i><span> 友鏈</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">常用的Lambda表达式</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2023-05-05T06:57:31.895Z" title="Created 2023-05-05 14:57:31">2023-05-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2023-08-02T04:59:57.848Z" title="Updated 2023-08-02 12:59:57">2023-08-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/kirito/categories/Java/">Java</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="常用的Lambda表达式"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h3 id="1-使用Lambda表达式进行集合遍历"><a href="#1-使用Lambda表达式进行集合遍历" class="headerlink" title="1. 使用Lambda表达式进行集合遍历"></a>1. 使用Lambda表达式进行集合遍历</h3><ul>
<li>未使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line"><span class="keyword">for</span> (String fruit : list) &#123;</span><br><span class="line">    System.out.println(fruit);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line">list.forEach(fruit -&gt; System.out.println(fruit));</span><br></pre></td></tr></table></figure>



<h3 id="2-使用Lambda表达式进行排序"><a href="#2-使用Lambda表达式进行排序" class="headerlink" title="2. 使用Lambda表达式进行排序"></a>2. 使用Lambda表达式进行排序</h3><ul>
<li>未使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line">Collections.sort(list, <span class="keyword">new</span> <span class="title class_">Comparator</span>&lt;String&gt;() &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">compare</span><span class="params">(String s1, String s2)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> s1.compareTo(s2);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<ul>
<li>使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line">Collections.sort(list, (s1, s2) -&gt; s1.compareTo(s2));</span><br></pre></td></tr></table></figure>



<h3 id="3-使用Lambda表达式进行过滤"><a href="#3-使用Lambda表达式进行过滤" class="headerlink" title="3. 使用Lambda表达式进行过滤"></a>3. 使用Lambda表达式进行过滤</h3><ul>
<li>未使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line">List&lt;String&gt; filteredList = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line"><span class="keyword">for</span> (String fruit : list) &#123;</span><br><span class="line">    <span class="keyword">if</span> (fruit.startsWith(<span class="string">&quot;a&quot;</span>)) &#123;</span><br><span class="line">        filteredList.add(fruit);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line">List&lt;String&gt; filteredList = list.stream()</span><br><span class="line">    .filter(fruit -&gt; fruit.startsWith(<span class="string">&quot;a&quot;</span>))</span><br><span class="line">    .collect(Collectors.toList());</span><br></pre></td></tr></table></figure>



<h3 id="4-使用Lambda表达式进行映射"><a href="#4-使用Lambda表达式进行映射" class="headerlink" title="4. 使用Lambda表达式进行映射"></a>4. 使用Lambda表达式进行映射</h3><ul>
<li>未使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line">List&lt;Integer&gt; lengths = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;Integer&gt;();</span><br><span class="line"><span class="keyword">for</span> (String fruit : list) &#123;</span><br><span class="line">    lengths.add(fruit.length());</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line">List&lt;Integer&gt; lengths = list.stream()</span><br><span class="line">    .map(fruit -&gt; fruit.length())</span><br><span class="line">    .collect(Collectors.toList());</span><br></pre></td></tr></table></figure>



<h3 id="5-使用Lambda表达式进行归约"><a href="#5-使用Lambda表达式进行归约" class="headerlink" title="5. 使用Lambda表达式进行归约"></a>5. 使用Lambda表达式进行归约</h3><ul>
<li>未使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line">Map&lt;Integer, List&lt;String&gt;&gt; grouped = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;Integer, List&lt;String&gt;&gt;();</span><br><span class="line"><span class="keyword">for</span> (String fruit : list) &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">length</span> <span class="operator">=</span> fruit.length();</span><br><span class="line">    <span class="keyword">if</span> (!grouped.containsKey(length)) &#123;</span><br><span class="line">        grouped.put(length, <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;());</span><br><span class="line">    &#125;</span><br><span class="line">    grouped.get(length).add(fruit);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<hr>
<ul>
<li>使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line">Map&lt;Integer, List&lt;String&gt;&gt; grouped = list.stream()</span><br><span class="line">    .collect(Collectors.groupingBy(fruit -&gt; fruit.length()));</span><br></pre></td></tr></table></figure>

<h3 id="6-使用Lambda表达式进行函数式接口的实现"><a href="#6-使用Lambda表达式进行函数式接口的实现" class="headerlink" title="6. 使用Lambda表达式进行函数式接口的实现"></a>6. 使用Lambda表达式进行函数式接口的实现</h3><ul>
<li>未使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">MyInterface</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">doSomething</span><span class="params">(String input)</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">MyInterface</span> <span class="variable">myObject</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">MyInterface</span>() &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">doSomething</span><span class="params">(String input)</span> &#123;</span><br><span class="line">        System.out.println(input);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<hr>
<ul>
<li>使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">MyInterface</span> <span class="variable">myObject</span> <span class="operator">=</span> input -&gt; System.out.println(input);</span><br><span class="line">myObject.doSomething(<span class="string">&quot;Hello World&quot;</span>);</span><br></pre></td></tr></table></figure>



<h3 id="7-使用Lambda表达式进行线程的创建"><a href="#7-使用Lambda表达式进行线程的创建" class="headerlink" title="7. 使用Lambda表达式进行线程的创建"></a>7. 使用Lambda表达式进行线程的创建</h3><ul>
<li>未使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Thread</span> <span class="variable">thread</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Thread</span>(<span class="keyword">new</span> <span class="title class_">Runnable</span>() &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Thread is running.&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">thread.start();</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Thread</span> <span class="variable">thread</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Thread</span>(() -&gt; System.out.println(<span class="string">&quot;Thread is running.&quot;</span>));</span><br><span class="line">thread.start();</span><br></pre></td></tr></table></figure>

<h3 id="8-使用Lambda表达式进行Optional的操作"><a href="#8-使用Lambda表达式进行Optional的操作" class="headerlink" title="8. 使用Lambda表达式进行Optional的操作"></a>8. 使用Lambda表达式进行Optional的操作</h3><ul>
<li>未使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">String</span> <span class="variable">str</span> <span class="operator">=</span> <span class="string">&quot;Hello World&quot;</span>;</span><br><span class="line"><span class="keyword">if</span> (str != <span class="literal">null</span>) &#123;</span><br><span class="line">    System.out.println(str.toUpperCase());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Optional&lt;String&gt; str = Optional.ofNullable(<span class="string">&quot;Hello World&quot;</span>);</span><br><span class="line">str.map(String::toUpperCase).ifPresent(System.out::println);</span><br></pre></td></tr></table></figure>

<h3 id="10-使用Lambda表达式进行Stream的流水线操作"><a href="#10-使用Lambda表达式进行Stream的流水线操作" class="headerlink" title="10. 使用Lambda表达式进行Stream的流水线操作"></a>10. 使用Lambda表达式进行Stream的流水线操作</h3><ul>
<li>未使用Lambda表达式：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line">List&lt;String&gt; filteredList = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line"><span class="keyword">for</span> (String fruit : list) &#123;</span><br><span class="line">    <span class="keyword">if</span> (fruit.startsWith(<span class="string">&quot;a&quot;</span>)) &#123;</span><br><span class="line">        filteredList.add(fruit.toUpperCase());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">Collections.sort(filteredList);</span><br></pre></td></tr></table></figure>

<ul>
<li>使用Lambda表达式：（下面的toList是再java16中引入的）</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line">List&lt;String&gt; filteredList  = list.stream()</span><br><span class="line">                .filter(fruit -&gt; fruit.startsWith(<span class="string">&quot;a&quot;</span>))</span><br><span class="line">                .map(String::toUpperCase)</span><br><span class="line">                .sorted()</span><br><span class="line">                .toList();</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://kirito2493381254.gitee.io/kirito">牵风散步の雲</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://kirito2493381254.gitee.io/kirito/2023/05/05/%E5%B8%B8%E7%94%A8%E7%9A%84Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F/">https://kirito2493381254.gitee.io/kirito/2023/05/05/%E5%B8%B8%E7%94%A8%E7%9A%84Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/kirito/tags/Java%E5%9F%BA%E7%A1%80/">Java基础</a></div><div class="post_share"><div class="social-share" data-image="/kirito/img/artical.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/kirito/2023/05/06/JDK/" title="各JDK新特性"><img class="cover" src="/kirito/img/artical5.jpg" onerror="onerror=null;src='/kirito/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">各JDK新特性</div></div></a></div><div class="next-post pull-right"><a href="/kirito/2023/04/25/VITS%E8%AE%BA%E6%96%87%E5%AF%BC%E8%AF%BB/" title="VITS导读"><img class="cover" src="/kirito/img/artical4.jpg" onerror="onerror=null;src='/kirito/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">VITS导读</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>Related Articles</span></div><div class="relatedPosts-list"><div><a href="/kirito/2023/05/06/JDK/" title="各JDK新特性"><img class="cover" src="/kirito/img/artical5.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-06</div><div class="title">各JDK新特性</div></div></a></div><div><a href="/kirito/2023/06/09/JMM/" title="JMM"><img class="cover" src="/kirito/img/artical5.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-06-09</div><div class="title">JMM</div></div></a></div><div><a href="/kirito/2023/04/17/Java%E5%8F%8D%E5%B0%84/" title="Java反射"><img class="cover" src="/kirito/img/artical2.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-17</div><div class="title">Java反射</div></div></a></div><div><a href="/kirito/2023/04/18/Java%E5%BA%8F%E5%88%97%E5%8C%96/" title="Java序列化"><img class="cover" src="/kirito/img/artical4.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-18</div><div class="title">Java序列化</div></div></a></div></div></div><hr class="custom-hr"/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> Comment</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/kirito/img/avatar.jpg" onerror="this.onerror=null;this.src='/kirito/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">牵风散步の雲</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/kirito/archives/"><div class="headline">Articles</div><div class="length-num">21</div></a><a href="/kirito/tags/"><div class="headline">Tags</div><div class="length-num">12</div></a><a href="/kirito/categories/"><div class="headline">Categories</div><div class="length-num">8</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Kiritoabc"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Kiritoabc" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:2493381254@qq.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E4%BD%BF%E7%94%A8Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%BF%9B%E8%A1%8C%E9%9B%86%E5%90%88%E9%81%8D%E5%8E%86"><span class="toc-number">1.</span> <span class="toc-text">1. 使用Lambda表达式进行集合遍历</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%BF%9B%E8%A1%8C%E6%8E%92%E5%BA%8F"><span class="toc-number">2.</span> <span class="toc-text">2. 使用Lambda表达式进行排序</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%BD%BF%E7%94%A8Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%BF%9B%E8%A1%8C%E8%BF%87%E6%BB%A4"><span class="toc-number">3.</span> <span class="toc-text">3. 使用Lambda表达式进行过滤</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E4%BD%BF%E7%94%A8Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%BF%9B%E8%A1%8C%E6%98%A0%E5%B0%84"><span class="toc-number">4.</span> <span class="toc-text">4. 使用Lambda表达式进行映射</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E4%BD%BF%E7%94%A8Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%BF%9B%E8%A1%8C%E5%BD%92%E7%BA%A6"><span class="toc-number">5.</span> <span class="toc-text">5. 使用Lambda表达式进行归约</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E4%BD%BF%E7%94%A8Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%BF%9B%E8%A1%8C%E5%87%BD%E6%95%B0%E5%BC%8F%E6%8E%A5%E5%8F%A3%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-number">6.</span> <span class="toc-text">6. 使用Lambda表达式进行函数式接口的实现</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-%E4%BD%BF%E7%94%A8Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%BF%9B%E8%A1%8C%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%88%9B%E5%BB%BA"><span class="toc-number">7.</span> <span class="toc-text">7. 使用Lambda表达式进行线程的创建</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-%E4%BD%BF%E7%94%A8Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%BF%9B%E8%A1%8COptional%E7%9A%84%E6%93%8D%E4%BD%9C"><span class="toc-number">8.</span> <span class="toc-text">8. 使用Lambda表达式进行Optional的操作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-%E4%BD%BF%E7%94%A8Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%BF%9B%E8%A1%8CStream%E7%9A%84%E6%B5%81%E6%B0%B4%E7%BA%BF%E6%93%8D%E4%BD%9C"><span class="toc-number">9.</span> <span class="toc-text">10. 使用Lambda表达式进行Stream的流水线操作</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/kirito/2023/08/09/MySQL%E4%BA%8B%E5%8A%A1%E7%AF%87/" title="MySQL事务"><img src="/kirito/img/artical3.jpg" onerror="this.onerror=null;this.src='/kirito/img/404.jpg'" alt="MySQL事务"/></a><div class="content"><a class="title" href="/kirito/2023/08/09/MySQL%E4%BA%8B%E5%8A%A1%E7%AF%87/" title="MySQL事务">MySQL事务</a><time datetime="2023-08-09T10:21:17.207Z" title="Created 2023-08-09 18:21:17">2023-08-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/kirito/2023/08/09/MySQL%E7%B4%A2%E5%BC%95%E7%AF%87/" title="MySQL索引"><img src="/kirito/img/artical5.jpg" onerror="this.onerror=null;this.src='/kirito/img/404.jpg'" alt="MySQL索引"/></a><div class="content"><a class="title" href="/kirito/2023/08/09/MySQL%E7%B4%A2%E5%BC%95%E7%AF%87/" title="MySQL索引">MySQL索引</a><time datetime="2023-08-09T08:20:58.443Z" title="Created 2023-08-09 16:20:58">2023-08-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/kirito/2023/08/04/go-zero/" title="go-zero"><img src="/kirito/img/artical.jpg" onerror="this.onerror=null;this.src='/kirito/img/404.jpg'" alt="go-zero"/></a><div class="content"><a class="title" href="/kirito/2023/08/04/go-zero/" title="go-zero">go-zero</a><time datetime="2023-08-04T08:43:36.925Z" title="Created 2023-08-04 16:43:36">2023-08-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/kirito/2023/06/26/Canal/" title="Canal"><img src="/kirito/img/artical4.jpg" onerror="this.onerror=null;this.src='/kirito/img/404.jpg'" alt="Canal"/></a><div class="content"><a class="title" href="/kirito/2023/06/26/Canal/" title="Canal">Canal</a><time datetime="2023-06-26T05:53:08.133Z" title="Created 2023-06-26 13:53:08">2023-06-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/kirito/2023/06/19/Lua/" title="lua"><img src="/kirito/img/artical2.jpg" onerror="this.onerror=null;this.src='/kirito/img/404.jpg'" alt="lua"/></a><div class="content"><a class="title" href="/kirito/2023/06/19/Lua/" title="lua">lua</a><time datetime="2023-06-19T04:42:42.400Z" title="Created 2023-06-19 12:42:42">2023-06-19</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/kirito/img/artical.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2023 By 牵风散步の雲</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="chat-btn" type="button" title="Chat"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="Scroll To Comments"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/kirito/js/utils.js"></script><script src="/kirito/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: '',
      appKey: '',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>